<?php
 jimport('joomla.application.component.view');
class svViewCanbo extends JView
{
	function display()
	{
		$task = JRequest::getCMD('task');
		switch ($task)
		{
			case 'add':
				$this->_add();
				break;
			case 'edit':
				$this->_edit();
				break;
			case 'save':
				$this->_save();
				break;
			case 'cancel':
				$this->_cancel();
				break;
			case 'remove':
				$this->_remove();
				break;
			default :
				$this->_default();
				break;
		}
		parent::display();
	}
	// hien thi chi tiet
	function _default()
	{
		// them toolbar
		$this->_gridToolbar();
		// ket noi voi model
		$model=$this->getModel('canbo');
		// kiem tra xem co phai la add hay edit form khong
		$action=JRequest::getVar('action');
		if($action=='') // vao default
		{
			// cac bien gia tri
			$page=JRequest::getInt('page');
			// xem co phai la xem chi tiet 1 can bo hay khong
			$idcanbo=JRequest::getVar('idcanbo');
			if($idcanbo==0)
			{
				$result=$model->getAllCanbo($page);
			}
			else 
			{
				$result=$model->getCanboByIDs($idcanbo);
			}
			$title='Quản lý các cán bộ';
			// set layout
			$this->assignRef('dataObject', $result);
			$this->assignRef('title', $title);
			$this->setLayout('default');
		}
		else
		{
			$ten=JRequest::getVar('ten');
			$this->assignRef('ten', $ten);
			$mieuta = JRequest::getVar('mieuta', '', 'POST','validation type',JREQUEST_ALLOWHTML);
			$this->assignRef('mieuta', $mieuta);
			$namsinh=JRequest::getVar('namsinh');
			$this->assignRef('namsinh', $namsinh);
			$quequan=JRequest::getVar('quequan');
			$this->assignRef('quequan', $quequan);
			$cmnd=JRequest::getVar('cmnd');
			$this->assignRef('cmnd', $cmnd);
			$gioitinh=JRequest::getVar('gioitinh');
			$this->assignRef('gioitinh', $gioitinh);
			$diachi=JRequest::getVar('diachi');
			$this->assignRef('diachi', $diachi);
			$dienthoai=JRequest::getVar('dienthoai');
			$this->assignRef('dienthoai', $dienthoai);
			$tongiao=JRequest::getVar('tongiao');
			$this->assignRef('tongiao', $tongiao);
			$honnhan=JRequest::getVar('honnhan');
			$this->assignRef('honnhan', $honnhan);
			$dantoc=JRequest::getVar('dantoc');
			$this->assignRef('dantoc', $dantoc);
			//-------- tbl quan ly chung
			$idphongban=JRequest::getInt('idphongban');
			$idchucvu=JRequest::getInt('idchucvu');
			$idcanbo=JRequest::getInt('idcanbo');
			$idchucdanh=JRequest::getInt('idchucdanh');
			// upload file anh
			$imgupload=$this->upload();
			if($imgupload=='') $imgupload='kaka.jpg';
			$this->assignRef('uploadimg', $imgupload);
			// select cac du lu di kem
			$listPhongban=$model->getAllPhongban();
			$listChucvu=$model->getAllChucvu();
			$listChucdanh=$model->getAllChucdanh();
			$this->assignRef('listPhongban', $listPhongban);
			$this->assignRef('listChucvu', $listChucvu);
			$this->assignRef('listChucdanh', $listChucdanh);
			if($action=='add')// them moi mot can bo
			{
				$this->_formToolbar('Thêm mới một cán bộ');
				// set layout
				$this->setLayout('add');
			}
			else // edit mot can bo
			{
				$this->assignRef('idcanbo', $idcanbo);
				// lay du lieu tu model
				$canbo=$model->getCanboByID($idcanbo);
				// set layout
				$this->assignRef('canbo', $canbo);
				// toolbar
				$this->_formToolbar('Chỉnh sửa một cán bộ');
				$this->setLayout('edit');
			}
		}

	}
	function _add()
	{
		header('location:index.php?option=com_svquanlycanbo&view=canbo&action=add');
	}
	function _edit()
	{
		// lay id tu form default
		$idlist    = JRequest::getVar('cid',array(0),'','array');
		JArrayHelper::toInteger($idlist, array(0));
		$idcanbo = $idlist[0];
		header('location:index.php?option=com_svquanlycanbo&view=canbo&action=edit&idcanbo='.$idcanbo);
	}
	function _save()
	{
		// lay du lieu cho tbl can bo
		$ten=JRequest::getVar('ten');
		$mieuta = JRequest::getVar('mieuta', 'Mieu ta ve phong ban', 'POST','validation type',JREQUEST_ALLOWHTML);
		 $namsinh=JRequest::getVar('namsinh');
		$quequan=JRequest::getVar('quequan');
		$cmnd=JRequest::getVar('cmnd');
		$gioitinh=JRequest::getVar('gioitinh');
		$diachi=JRequest::getVar('diachi');
		$dienthoai=JRequest::getVar('dienthoai');
		$tongiao=JRequest::getVar('tongiao');
		$honnhan=JRequest::getVar('honnhan');
		$hinhanh=JRequest::getVar('hinhanh');
		$dantoc=JRequest::getVar('dantoc');
		//-------- tbl quan ly chung
		$idphongban=JRequest::getInt('idphongban');
		$idcanbo=JRequest::getInt('idcanbo');
		$idchucvu=JRequest::getInt('idchucvu');
		$idchucdanh=JRequest::getInt('idchucdanh');
		// xu ly yeu cau edit/insert
		$model=$this->getModel('canbo');
		if($idcanbo==0)// xac thuc viec them mot item
		{
			$idcanbo=$model->getMaxIDCanbo();
			$idcanbo++;
			$exec=$model->addNewCanbo($ten,$namsinh,$quequan,$cmnd,$gioitinh,$diachi,$dienthoai,$tongiao,$honnhan,$hinhanh,$mieuta,$dantoc);
			$exec2=$model->addNewQuanlychung($idphongban,$idchucvu,$idchucdanh,$idcanbo);
			echo $exec;
			echo $exec2;
		}
		else//xac thuc viec edit mot item
		{
			$idqlc=JRequest::getInt('idqlc');
			$exec=$model->editCanbo($idcanbo,$ten,$namsinh,$quequan,$cmnd,$gioitinh,$diachi,$dienthoai,$tongiao,$honnhan,$hinhanh,$mieuta,$dantoc);
			$exec2=$model->editQuanlychung($idphongban,$idchucvu,$idchucdanh,$idqlc);
			echo $exec;
			echo $exec2;
		}
		header('location:index.php?option=com_svquanlycanbo&view=canbo');
	}
	function _cancel()
	{
		header('Location:index.php?option=com_svquanlycanbo&view=canbo');
	}
	function _remove()
	{
		// lay id tu form default
		$idlist    = JRequest::getVar('cid',array(0),'','array');
		JArrayHelper::toInteger($idlist, array(0));
		$idchucvu    = $idlist[0];
		// thuc hien yeu cau remove
		$model=$this->getModel('chucdanh');
		$exec=$model->delChucvu($idchucvu);
		echo $exec;
		$this->_default();
		
	}
	// them cac button tren thanh toolbar
	function _gridToolbar()
	{
		JToolBarHelper::title('Quản lý các chức danh.!');
		JToolBarHelper::addNew();
		JToolBarHelper::editListX();
		JToolBarHelper::deleteList();
	}
	function _formToolbar($title)
	{
		JToolBarHelper::title($title);
		JToolBarHelper::save();
		JToolBarHelper::cancel();
	}
	function upload()
	{
		jimport('joomla.client.helper');
		$allowedExts = array("jpg", "jpeg", "gif", "png");
		$extension = end(explode(".", $_FILES["file"]["name"]));
		if ((($_FILES["file"]["type"] == "image/gif")
				|| ($_FILES["file"]["type"] == "image/jpeg")
				|| ($_FILES["file"]["type"] == "image/png")
				|| ($_FILES["file"]["type"] == "image/pjpeg"))
				&& ($_FILES["file"]["size"] < 100000)
				&& in_array($extension, $allowedExts))
		{
			if ($_FILES["file"]["error"] > 0)
			{
				echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
			}
			else
			{
				//echo "Upload: " . $_FILES["file"]["name"] . "<br />";
				//echo "Type: " . $_FILES["file"]["type"] . "<br />";
				//echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
				//echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
				$folder=JPATH_ADMINISTRATOR.DS."components".DS."com_svquanlycanbo".DS."uploads";
				if (file_exists($folder."/" . $_FILES["file"]["name"]))
				{
					echo $_FILES["file"]["name"] . " already exists. ";
				}
				else
				{
					move_uploaded_file($_FILES["file"]["tmp_name"],
					$folder."/" . $_FILES["file"]["name"]);
					return $_FILES["file"]["name"];
				}
			}
		}
		else
		{
			//echo "Invalid file";
		}	
    }
}
?>